package com.sczmgk.hotline_pzh.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.sczmgk.hotline_pzh.utils.CommonUtils;

import java.util.HashMap;
import java.util.Map;
public class CustomRing extends View {

	protected static final String TAG = "CustomRing";
	/**
	 * 水波纹的颜色
	 */
	private int color = 0x386abb;
	private int volumeColor = 0x386abb;
	/**
	 * 圆心的X坐标
	 */
	private float cx;//
	/**
	 * 圆心的Y坐标
	 */
	private float cy;//
	private int minRadius ;
	private int maxRadius ;
	private int paintCount = 3;
	/**
	 * 音量的半径
	 */
	private int volumeRadius ;
	/**
	 * 圆与圆的差距
	 */
	private int radiusDis;
	/**
	 * 圆与圆透明度的差距
	 */
	private int alphaDis;
	/**
	 *  圆最大变化的距离
	 */
	private int radiusMaxChangeDis;
	private int maxAlpha;
	private int alpha1;
	private int alpha2;
	private int alpha3;
	private int alpha4;
	/**
	 * 圆的半径
	 */
	private float radius1;//
	private float radius2;//
	private float radius3;//
	private float radius4;//
	/**
	 * 画笔线条的宽度
	 */
	private float strokewidth = 2;//
	private float volumeWidth = 2;
	/**
	 * 音量的画笔
	 */
	private Paint volumePaint;


	private Map<String, Paint> paintMaps = new HashMap<String,Paint>();

	public CustomRing(Context context) {
		super(context);
		//  Auto-generated constructor stub
	}

	public CustomRing(Context context, AttributeSet attrs) {
		super(context, attrs);
		//取得屏幕的像素密度
		float screenDensity = CommonUtils.getScreenDensity(context);
//		float width = CommonUtils.getScreenWidth(context);
//		float height = CommonUtils.getScreenHeight(context);
//		MyLog.d(TAG,"像素密度是： "+screenDensity+"   密度是： "+ width+ "   高度是： "+height);
		minRadius = (int)(86*(screenDensity/2));//最小圆的半径
		maxRadius = (int)(206*(screenDensity/2));//最大圆的半径
		volumeRadius = (int)(86*(screenDensity/2));//音量的半径
//		MyLog.i(TAG,"最小半径： "+minRadius+"   最大半径是： "+maxRadius+ "   音量的半径是： "+volumeRadius);
		initpaint();//
		
	}
	/**
	 * 初始化画笔 初始化画笔. 实例化对象只能在方法内完成 注意要在点击事件与 构造方法中都要初始化
	 *
	 */
	private void initpaint() {
		//计算出第一个圆的透明度
		
		radiusDis = (maxRadius - minRadius)/(paintCount);
		
		radiusMaxChangeDis = (maxRadius - minRadius);
		
		maxAlpha = 240;
		alphaDis = maxAlpha/paintCount;
		alpha1 = maxAlpha;
		alpha2 = alpha1 - alphaDis;
		alpha3 = alpha2 - alphaDis;
		alpha4 = alpha3 - alphaDis;
		
		radius1 = minRadius;
		radius2 = radius1 + radiusDis;
		radius3 = radius2 + radiusDis;
		radius4 = radius3 + radiusDis;
//		MyLog.d(TAG,"圆与圆的差距   = "+radiusDis+"       透明度的差距 = "+alphaDis);	
//		MyLog.i(TAG,"初始化的透明度： 1 = "+alpha1+ "    2 = "+alpha2+ "    3 = "+alpha3+ "    4 = "+alpha4);
				
		for (int i = 1; i < paintCount+1; i++) {
			paintMaps.put("paint"+i, createPaint(i));
		}		
//		MyLog.d(TAG,"画笔的数量： "+paintMaps.size());	
		
		//初始化音量画笔
		volumePaint = new Paint();
		volumePaint.setAntiAlias(true);// 打开抗钜齿
		volumePaint.setColor(volumeColor);// 设置颜色
		volumePaint.setStyle(Style.STROKE);// 设置画线条
		volumePaint.setStrokeWidth(volumeWidth);
		volumePaint.setAlpha(127);// 画笔的透明度0-255 (0 为全透明)
	}
	/**
	 * 生成画笔
	 * @param flag 画笔的标识（用于设置透明度）
	 * @return 对应的画笔
	 */
	private Paint createPaint(int flag){
	    Paint paint = new Paint();
		paint.setAntiAlias(true);// 打开抗钜齿
		paint.setColor(color);// 设置颜色
		paint.setStyle(Style.STROKE);// 设置画线条
		paint.setStrokeWidth(strokewidth);
		int currentAlpha = createAlpha(flag);
		paint.setAlpha(currentAlpha);// 画笔的透明度0-255 (0 为全透明)
	    return paint;	
	}

    /**
     * 生成画笔的透明度
     * @param flag
     * @return 透明度
     */
	private int createAlpha(int flag){
		int alpha = 0;
		switch (flag) {
		case 1:
			alpha = alpha1;
			break;
		case 2:
			alpha = alpha2;
			
			break;
		case 3:
			alpha = alpha3;
			
			break;
		case 4:
			
			alpha = alpha4;
			break;

		default:
			break;
		}
		return alpha;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(cx, cy, volumeRadius, volumePaint);
		canvas.drawCircle(cx, cy, radius1, paintMaps.get("paint1"));
		canvas.drawCircle(cx, cy, radius2, paintMaps.get("paint2"));
		canvas.drawCircle(cx, cy, radius3, paintMaps.get("paint3"));
//		canvas.drawCircle(cx, cy, radius4, paintMaps.get("paint4"));
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		cx = this.getWidth()/2;
		cy = this.getHeight()/2;
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			handler.sendEmptyMessageDelayed(0, 50);
			flushState();//
			invalidate();
		};
	};

	private void flushState() {
		if(volumeWidth>2){
			volumeWidth -= 3;
		}
		volumePaint.setStrokeWidth(volumeWidth);
		alpha1 -= getAlphaDis(radiusMaxChangeDis);
		radius1 += 1;
		paintMaps.get("paint1").setAlpha(alpha1);
		if (radius1 > maxRadius) {
//			MyLog.e(TAG, "重设第一个圆");
			alpha1 = maxAlpha;
			radius1 = minRadius;
//			MyLog.d(TAG,"半径："+radius1+ "  透明度： "+alpha1);
		}

		alpha2 -= getAlphaDis(radiusMaxChangeDis);
		radius2 += 1;
		paintMaps.get("paint2").setAlpha(alpha2);
		if (radius2 > maxRadius) {
//			MyLog.e(TAG, "重设第二个圆");
			alpha2 = maxAlpha;
			radius2 = minRadius;
//			MyLog.d(TAG,"半径："+radius2+ "  透明度： "+alpha2);
		}

		alpha3 -= getAlphaDis(radiusMaxChangeDis);
		radius3 += 1;
		paintMaps.get("paint3").setAlpha(alpha3);
		if (radius3 > maxRadius) {
//			MyLog.e(TAG, "重设第三个圆");
			alpha3 = maxAlpha;
			radius3 = minRadius;
//			MyLog.d(TAG,"半径："+radius3+ "  透明度： "+alpha3);
		}

//		alpha4 -= 2;
//		radius4 += 1;
//		paintMaps.get("paint4").setAlpha(alpha4);
//		if (radius4 > maxRadius) {
//			MyLog.e(TAG, "重设第四个圆");
//			alpha4 = maxAlpha;
//			radius4 = minRadius;
//		}
		
//		MyLog.i(TAG,"变化 后的透明度： 1 = "+alpha1+ "    2 = "+alpha2+ "    3 = "+alpha3+ "    4 = "+alpha4);	
//		MyLog.d(TAG,"变化 后的    半径： 1 = "+radius1+ "    2 = "+radius2+ "    3 = "+radius3+ "    4 = "+radius4);	
	}

	/**
	 * 开始动画
	 */
	public void startAnimation() {
		handler.sendEmptyMessage(0);
	}

	/**
	 * 停止动画
	 */
	public void stopAnimation() {
		handler.removeCallbacksAndMessages(null);
	}
	/**
	 * 设置音量画笔的宽度
	 * @param volume
	 */
	public void setVolume(float volume){
//		MyLog.i(TAG,"设置音量画笔音量为 ： "+volume);
		//  加上音量
		volumeWidth =2;
		volumeWidth += volume*4;
	}
	/**
	 * 取得秀明度变化的等级
	 * @param maxCount
	 * @return 透明度
	 */
	public int getAlphaDis(int maxCount){
		
		return 240/maxCount;
	}
}
